#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include "user_data.h"
#include "wasApp.h"



static int userdata[USERDATA_TOTAL] = {0};

static char userdata_name[USERDATA_TOTAL][32] = 
{
    "Unit",
    "Floor",
    "Index",
    "GateLockTime"
};

int *userdata_get(USERDATAINDEX index)
{
    return &userdata[index];
}

bool userdata_save(void)
{
    FILE *fp = fopen(FILE_USER_DATA_PATH, "w+");
    if(!fp)
    {
        LOG_RED("Err: can t open file:%s\n", FILE_RFID_DATA_PATH);
        return false;
    }

    for(int i = 0; i < USERDATA_TOTAL; i++)
    {
        fprintf(fp, "%s=%d\n", userdata_name[i], userdata[i]);
    }

    fclose(fp);
    system("sync");
    return true;
}

bool userdata_reset(void)
{
    LOG_CYAN("userdata reset!!!!\n");
    userdata[USERDATA_UNIT]  = 1;
    userdata[USERDATA_FLOOR] = 1;
    userdata[USERDATA_INDEX] = 1;
    userdata[USERDATA_GATE_LOCK_TIME] = 3;

    return userdata_save();
    
}


bool userdata_init(void)
{
    FILE *fp = fopen(FILE_USER_DATA_PATH, "r+");
    if(!fp)
    {
        LOG_RED("Err: can t open file:%s\n", FILE_USER_DATA_PATH);
        
        
        userdata_reset();
        return false;
    }

    char buf[32];
    int read_count = 0;

    while((fgets(buf, sizeof(buf), fp)))
    {
        int line_len = strlen(buf);
        if('\n' == buf[line_len-1])
        {
            buf[line_len-1] = '\0';
        }


        
        
        // LOG_WHITE("'%s'\n", buf);


        char *pstr      = NULL;
        char name[32]={0};
        int  value=0;

        if((pstr=strstr(buf, userdata_name[read_count])))
        {
            sscanf(pstr, "%[A-z]=%d", name, &value);
            userdata[read_count] = value;
            LOG_WHITE("%s = %d\n", name, value);
        }

        read_count++;        
    }



    fclose(fp);
    return true;
}




static int RFID_CARD_TOTAL = 0;
static unsigned int RFID_CARD_ARR[50];

/*************************************************************************
 * @brief  卡片列表初始化
 * @date   2022-11-15 12:01
 * @author xiaoele
 **************************************************************************/
bool _init_rfid_data(void)
{
	RFID_CARD_TOTAL = 0;
	memset(RFID_CARD_ARR, 0, sizeof(RFID_CARD_ARR));

    FILE *fp = fopen(FILE_RFID_DATA_PATH, "r+");
    if(!fp)
    {
        LOG_RED("Err: can t open file:%s\n", FILE_RFID_DATA_PATH);
        
        fopen(FILE_RFID_DATA_PATH, "w+");
        return false;
    }
    
    char buf[16];
    while((fgets(buf, sizeof(buf), fp)))
    {   
        int line_len = strlen(buf);
        if('\n' == buf[line_len-1])
        {
            buf[line_len-1] = '\0';
        }

        // LOG_WHITE("buf[%d]='%s'\n", RFID_CARD_TOTAL, buf);
        RFID_CARD_ARR[RFID_CARD_TOTAL] = atoi(buf);
        RFID_CARD_TOTAL++;
    }
    fclose(fp);

    LOG_GREEN("RFID TOTAL=%d\n", RFID_CARD_TOTAL);




	return true;
}

/*************************************************************************
 * @brief  保存卡片数据到本地文件
 * @date   2023-05-03 10:55
 * @author xiaole
 **************************************************************************/
bool rfid_data_save(void)
{
    FILE *fp = fopen(FILE_RFID_DATA_PATH, "w+");
    if(!fp)
    {
        LOG_RED("Err: can t open file:%s\n", FILE_RFID_DATA_PATH);
        return false;
    }
         
    for(int i = 0; i < RFID_CARD_TOTAL; i++)
    {
        fprintf(fp, "%u\n", RFID_CARD_ARR[i]);
    }

    fclose(fp);

    system("sync");
    return true;
}


/*************************************************************************
 * @brief  添加卡片数据
 * @date   2023-05-03 10:55
 * @author xiaole
 * @param  numb - 卡号/数据 
 **************************************************************************/
bool rfid_data_add(unsigned int numb)
{
    if(is_rfid_data_exists(numb) == true)
    {
        LOG_YELLOW("Err: numb='%u' is exists\n", numb);
        return false;
    }

	if (RFID_CARD_TOTAL >= RFID_CARD_MAX)
	{
        LOG_RED("Err:total=%d, Max=%d, data is full\n", RFID_CARD_TOTAL, RFID_CARD_MAX);
		return false;
	}

	RFID_CARD_ARR[RFID_CARD_TOTAL] = numb;
	RFID_CARD_TOTAL++;


    rfid_data_save();
	return true;
}

/*************************************************************************
 * @brief  获取序号对应的 卡号
 * @date   2022-11-15 11:57
 * @author xiaoele
 * @param  index 下标
 **************************************************************************/
unsigned int rfid_data_get_number(int index)
{
	if (index >= RFID_CARD_TOTAL)
	{
        LOG_B_WHITE("total:%d, %d\n", RFID_CARD_TOTAL, index);
		return 0;
	}

	return RFID_CARD_ARR[index];
}

/*************************************************************************
 * @brief  检测数据是否存在
 * @date   2023-01-31 16:09
 * @author xiaole
 * @return true -  数据存在
 * @return false - 数据不存在
 **************************************************************************/
bool is_rfid_data_exists(unsigned int numb)
{
    for(int i = 0; i < RFID_CARD_TOTAL; i++)
    {
        if(numb == rfid_data_get_number(i))
        {			
            return true;
        }

    }


    return false;
}


/*************************************************************************
 * @brief  根据下标 删除卡片数据
 * @date   2023-05-03 10:56
 * @author xiaole
 * @return 
 * @param  index - 需要删除的下标
 **************************************************************************/
bool rfid_data_del(int index)
{
    if(RFID_CARD_TOTAL < index)
    {
        LOG_RED("index bigger than total\n");
        return false;
    }

    LOG_WHITE("del [%d]-[%08X]-[%08d]\n", index, RFID_CARD_ARR[index], RFID_CARD_ARR[index]);
    
    for(int i = index; i < RFID_CARD_TOTAL; i++)
    {

		RFID_CARD_ARR[i] = RFID_CARD_ARR[i+1];
    }

	RFID_CARD_ARR[RFID_CARD_TOTAL] = 0;	    
    RFID_CARD_TOTAL--;
    
	rfid_data_save();
    return true;
}


/*************************************************************************
 * @brief  将rfid数据同步到室内机
 * @date   2023-05-03 13:53
 * @author xiaole
 * @return 
 * @param  
 * @note   
 **************************************************************************/
bool rfid_data_sync(void)
{


    return true;
}